package com.sat.pay.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.sat.common.entity.Result;
import com.sat.pay.config.PayRabbitMqConfig;
import com.sat.pay.service.WxPayService;
//import jakarta.servlet.ServletInputStream;
//import jakarta.servlet.http.HttpServletRequest;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wxPay")
public class WxPayController {

    @Autowired
    private WxPayService wxPayService;
    //完成调用统一下单接口 ,native指的是二维码支付

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *二维码支付所需要的参数
     * @param body  商品描述
     * @param outTradeNo 订单号
     * @param totalFee  价格
     * @return
     */
    @RequestMapping("native")
    public Result wxPaynative(String body,String outTradeNo,Integer totalFee){
        Map<String,String > map= wxPayService.wxPaynative(body,outTradeNo,totalFee);
        return Result.OK(map);

    }

    /**
     * 根据订单号查询支付结果
     *
     *
     * @param outTradeNo
     * @return
     */
    @GetMapping("find/payStatus")
    public Result payStatus(String outTradeNo){
        Map<String,String> map=wxPayService.payStatus(outTradeNo);
        return Result.OK(map);

    }

    /**
     * 根据订单号去取消订单
     * @param outTradeNo
     * @return
     *
     */
    @RequestMapping("closeorder")
    public Result closeorder(String outTradeNo){
        Map<String,String> map= wxPayService.closeorder(outTradeNo);
        return Result.OK(map);
    }

    /**
     * 用来接收微信传过来的异步支付结果
     * 使用内网床头，是我们的端口可以被微信可访问到，这样微信才能将消息传到我们当前接口
     * http://qq18307096730.gnway.cc:64107//wxPay/find/payStatus?outTradeNo=1505806967430463485
     * @param request
     * @return
     */
    @RequestMapping("notifyurl")
    public String notifyurl(HttpServletRequest request){
        try {
            ServletInputStream is = request.getInputStream();
            System.out.println(is);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            //将输入流的数据保存到输出流里面
            byte[] bytes=new byte[1024];
            int len = 0;
            while ((len=is.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
            byte[] bytes1 = outputStream.toByteArray();
            String s = new String(bytes1);
            //将支付结果xml格式的数据转为json发送到mq
            Map<String, String> map = WXPayUtil.xmlToMap(s);
            ObjectMapper mapper = new ObjectMapper();
            String jsonData = mapper.writeValueAsString(map);

            rabbitTemplate.convertAndSend(PayRabbitMqConfig.PAY_TOPIC_EXCHANGE,"pay.sat.lingyi",jsonData);


            outputStream.close();
            is.close();
            //将字节数组转为字符串
            System.out.println("返回的数据"+s);

            //获取到治国结果以后就给微信返回一个确认信息，不然微信一直传输局过来
            Map<String,String >map1=new HashMap<>();
            map1.put("return_code","SUCCESS");
            String s1 = WXPayUtil.mapToXml(map1);
            return s1;//这里return是返回给微信，因为微信将消息传到这个借口

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
